package dao;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import object.PageBean;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import pojo.LogIncome;
import pojo.LogNoruserbill;
import pojo.ZcArea;
import pojo.ZcItem;
import pojo.ZcOrder;
import pojo.ZcOrderItem;
import pojo.ZcOrderLog;
import pojo.ZcUser;
import config.ConstantConfig;
import config.Sql2zcConfig;
import config.SqlConfig;

/**
 * 
 * @author zgy
 *
 */
@Repository
public class Transaction2zcDao {
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	static Logger logger = Logger.getLogger(Transaction2zcDao.class.getName());
	
	/**
	 * 获取早餐配送区域的分页
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public PageBean<ZcArea> queryLandmark(String city,Integer curPage,Integer pageSize){
		PageBean<ZcArea> page=null;
		List<ZcArea> list =null;
		Object[] params = new Object[]{city,(curPage-1)*pageSize,pageSize};
		list = jdbcTemplate.query(Sql2zcConfig.queryAreas, new ZcArea(), params);
		Object[] countParams = new Object[]{city};
		Integer count=jdbcTemplate.queryForObject(Sql2zcConfig.queryAreasCount,countParams,Integer.class);
		page=new PageBean<ZcArea>(curPage, pageSize, count, list);
		return page;
	}
	
	
	/**
	 * 获取早餐配送区域的分页
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public ZcArea queryZcArea(Integer areaId){
		ZcArea zcArea=null;
		Object[] params = new Object[]{areaId};
		List<ZcArea> list = jdbcTemplate.query(Sql2zcConfig.queryAreasByAreaId, params, new ZcArea());
		if(list.size()>0){
			zcArea=list.get(0);
		}
		return zcArea;
	}
	
	/**
	 * 根据配送地区Id获取星期一到星期五早餐列表
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public List<ZcItem> queryZcItems(Integer landmarkId){
		Object[] params = new Object[]{landmarkId};
		List<ZcItem> list = jdbcTemplate.query(Sql2zcConfig.queryZcItemsBylandmarkId, new ZcItem(), params);
		return list;
	}
	
	/**
	 * 查询交易订单
	 * @param platformId
	 * @return
	 */
	public List<ZcOrder> queryZcOrder(Integer userId,String status ){
		Object[] params = new Object[]{userId,status};
		List<ZcOrder> list = jdbcTemplate.query(Sql2zcConfig.queryZcOrder, new ZcOrder(), params);
		return list;
	}
	
	/**
	 * 根据查询交易订单
	 * @param platformId
	 * @return
	 */
	public ZcOrder queryZcOrderByNum(String Num){
		ZcOrder order=null;
		Object[] params = new Object[]{Num};
		List<ZcOrder> list = jdbcTemplate.query(Sql2zcConfig.queryZcOrderByNum, params, new ZcOrder());
		if(list.size()>0){
			order=list.get(0);
		}
		return order;
	}
	
	/**
	 *  查询完成的交易订单的分页
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public PageBean<ZcOrder> queryZcOrderCompleted(Integer userId,Integer curPage,Integer pageSize){
		PageBean<ZcOrder> page=null;
		List<ZcOrder> list =null;
		Object[] params = new Object[]{userId,(curPage-1)*pageSize,pageSize};
		list = jdbcTemplate.query(Sql2zcConfig.queryZcOrderCompleted, new ZcOrder(), params);
		Object[] countParams = new Object[]{userId};
		Integer count=jdbcTemplate.queryForObject(Sql2zcConfig.queryZcOrderCompletedCount,countParams,Integer.class);
		page=new PageBean<ZcOrder>(curPage, pageSize, count, list);
		return page;
	}
	
	/**
	 * 新增交易订单
	 * @param transaction
	 * @return
	 */
	public boolean saveZcOrder(ZcOrder e) {
		Object[] params = e.insertObjecParams();
		if(jdbcTemplate.update(Sql2zcConfig.insertZcOrder, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 交易订单支付
	 * @param orderNum
	 * @return
	 */
	public boolean payZcorder(String orderNum) {
		Timestamp now=new Timestamp(new Date().getTime());
		Object[] params = new Object[]{ConstantConfig.TRANSACTION_PAYED,now,orderNum};
		if(jdbcTemplate.update(Sql2zcConfig.editZcorderPayStatus, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 交易订单结束
	 * @param orderNum
	 * @return
	 */
	public boolean endZcorder(String orderNum) {
		Timestamp now=new Timestamp(new Date().getTime());
		Object[] params = new Object[]{now,orderNum};
		if(jdbcTemplate.update(Sql2zcConfig.editZcorderStatus, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 查询交易订单子项
	 * @param platformId
	 * @return
	 */
	public List<ZcOrderItem> queryZcOrderItems(String orderNum ){
		Object[] params = new Object[]{orderNum};
		List<ZcOrderItem> list = jdbcTemplate.query(Sql2zcConfig.queryZcOrderItems, new ZcOrderItem(), params);
		return list;
	}
	

	
	
	/**
	 * 查询快递员的配送订单
	 * @param platformId
	 * @return
	 */
	public PageBean<ZcOrderItem> querySendItems(Integer suerId,Timestamp date,Timestamp date1,Integer curPage,Integer pageSize){
		PageBean<ZcOrderItem> page=null;
		//list = jdbcTemplate.query(Sql2zcConfig.querySendItems, new ZcOrderItem(), params);
		final List<ZcOrderItem> list = new ArrayList<ZcOrderItem>();
		Object[] params = new Object[]{suerId,date,date1,(curPage-1)*pageSize,pageSize};
		jdbcTemplate.query(Sql2zcConfig.queryZcSendItems, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				ZcOrderItem e = new ZcOrderItem();
				e.setId(rs.getInt("id"));
				e.setGname(rs.getString("gname"));
				e.setGcount(rs.getInt("gcount"));
				e.setPayStatus(rs.getString("pay_status"));
				e.setUserArea(rs.getString("user_area"));
				e.setUserAddress(rs.getString("user_address"));
				e.setUserName(rs.getString("user_name"));
				e.setUserMp(rs.getString("user_mp"));
				list.add(e);
			}
		});
		Object[] countParams = new Object[]{suerId,date};
		Integer count=jdbcTemplate.queryForObject(Sql2zcConfig.queryZcSendItemsCount,countParams,Integer.class);
		page=new PageBean<ZcOrderItem>(curPage, pageSize, count, list);
		return page;
	}
	
	
	/**
	 * //查询快递员根据用户手机查询订单
	 * @param platformId
	 * @return
	 */
	public List<ZcOrderItem> querySendItemsByMp(Integer suerId,Timestamp date,String userMp){
		final List<ZcOrderItem> list = new ArrayList<ZcOrderItem>();
		Object[] params = new Object[]{userMp+"%",suerId,date};
		//int[] paramsType = new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.TIMESTAMP};
		jdbcTemplate.query(Sql2zcConfig.queryZcSendItemsByMp, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				ZcOrderItem e = new ZcOrderItem();
				e.setId(rs.getInt("id"));
				e.setGname(rs.getString("gname"));
				e.setGcount(rs.getInt("gcount"));
				e.setPayStatus(rs.getString("pay_status"));
				e.setStatus(rs.getString("status"));
				e.setUserArea(rs.getString("user_area"));
				e.setUserAddress(rs.getString("user_address"));
				e.setUserName(rs.getString("user_name"));
				e.setUserMp(rs.getString("user_mp"));
				list.add(e);
			}
		});
		return list;
	}
	
	/**
	 * //查询快递员根据用户手机查询订单
	 * @param platformId
	 * @return
	 */
	public ZcOrderItem querySendItemById(Integer id){
		ZcOrderItem e=null;
		final List<ZcOrderItem> list = new ArrayList<ZcOrderItem>();
		Object[] params = new Object[]{id};
		jdbcTemplate.query(Sql2zcConfig.queryZcSendItemById, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				ZcOrderItem e = new ZcOrderItem();
				e.setId(rs.getInt("id"));
				e.setPayStatus(rs.getString("pay_status"));
				e.setStatus(rs.getString("status"));
				e.setOrderNum(rs.getString("order_num"));
				e.setSendTime(rs.getTimestamp("send_time"));
				list.add(e);
			}
		});
		if(list.size()>0){
			e=list.get(0);
		}
		return e;
	}
	
	//查询订单的子项数
	public Integer querySendItemCountByNum(String orderNum){
		Object[] countParams = new Object[]{orderNum};
		Integer count=jdbcTemplate.queryForObject(Sql2zcConfig.querySendItemsCountByNum,countParams,Integer.class);
		return count;
	}
	
	
	/**
	 * 新增早餐交易订单子项
	 * @param transaction
	 * @return
	 */
	public boolean saveZcOrderItem(List<ZcOrderItem> list) {
		StringBuffer sqlbf = new StringBuffer(
				"insert into zc_orderItem(gid,gname,gprice,gcount,order_num,create_time,send_time,user_id,user_mp,user_name,user_areaId,user_area,user_address) values ");
		for (ZcOrderItem item : list) {
			sqlbf.append(item.insertValuses());
		}
		String sql=sqlbf.substring(0, sqlbf.length()-1);
		if(jdbcTemplate.update(sql) != 0)
			return true;
		return false;
	}
	
	/**
	 * 结束配送订单
	 * @param orderNum
	 * @return
	 */
	public boolean endZcSendOrder(Integer id) {
		Timestamp now=new Timestamp(new Date().getTime());
		Object[] params = new Object[]{now,id};
		if(jdbcTemplate.update(Sql2zcConfig.editZcorderItemStatus, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 新增交易日志
	 * @param transactionLog
	 * @return
	 */
	public boolean saveZcOrderLog(ZcOrderLog e) {
		Object[] params = e.insertObjecParams();
		if(jdbcTemplate.update(Sql2zcConfig.insertZcOrderLog, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 查询交易日志
	 * @param transactionLog
	 * @return
	 */
	public List<ZcOrderLog> queryZcOrderLog(String orderNum ) {
		Object[] params = new Object[]{orderNum};
		List<ZcOrderLog> list = jdbcTemplate.query(Sql2zcConfig.queryZcOrderLog, new ZcOrderLog(), params);
		return list;
	}
	
	

	
	
	

	
	
	
	
	

	
	
	
	
	
	
	
	

	
	/**
	 * 查询充值状态
	 * @param orderNum
	 * @return
	 */
	public String queryLogIncomeStatus(String orderNum){
		Object[] params = new Object[]{orderNum};
		String status=jdbcTemplate.queryForObject(SqlConfig.queryLogncomeStatus, params, String.class);
		return status;
	}
	
	
	
	/**
	 * 查询账户余额
	 * @param orderNum
	 * @return
	 */
	public BigDecimal queryUserBalance(String openId){
		Object[] params = new Object[]{openId};
		BigDecimal balance=jdbcTemplate.queryForObject(Sql2zcConfig.queryUserBalance, params, BigDecimal.class);
		return balance;
	}
	
	/**
	 * 查询用户扣费表的个数
	 * @param orderNum
	 * @return
	 */
	public Integer queryNorUserBillCount(String orderNum){
		Object[] params = new Object[]{orderNum};
		Integer count=jdbcTemplate.queryForObject(Sql2zcConfig.queryLogNorUserBillCount, params, Integer.class);
		return count;
	}
	
	/**
	 * 插入用户扣费记录
	 * @param orderNum
	 * @return
	 */
	public boolean insertNorUserBill(LogNoruserbill noruserbill ){
		Object[] params = new Object[]{noruserbill.getLogNorUserBillUserID(),noruserbill.getLogNorUserBillUpdateTime(),noruserbill.getLogNorUserBillMoney(),
				noruserbill.getLogNorUserBillBalance(),noruserbill.getLogNorUserBillContentType(),noruserbill.getLogNorUserBillContent(),noruserbill.getLogNorUserBillPayment(),
				noruserbill.getLogNorUserBillPayMobile(),noruserbill.getLogNorUserBillInSeq(),noruserbill.getLogNorUserBillType(),noruserbill.getLogNorUserStatus()};
		if(jdbcTemplate.update(Sql2zcConfig.insertNorUserBill, params) != 0)
			return true;
		return false;
	}
	
	/**
	 *插入入账表记录
	 * @param orderNum
	 * @return
	 */
	public boolean insertLogIncome(LogIncome income){
		Object[] params = new Object[]{income.getLogIncomeTypeId(),income.getLogIncomeOrgId(),income.getLogIncomeOrgName(),
				income.getLogIncomeAgentId(),income.getLogIncomeUserId(),income.getLogIncomeCreateTime(),income.getLogIncomeMoney(),
				income.getLogIncomeFeedbackMoney(),income.getLogIncomeChannel(),income.getLogIncomeInSeq(),income.getLogIncomeStatus()};
		if(jdbcTemplate.update(Sql2zcConfig.insertLogIncome, params) != 0)
			return true;
		return false;
	}
	
	
	/**
	 * 更新用户扣费表状态
	 * @param orderNum
	 * @return
	 */
	public boolean updateNorUserBillStatus(String orderNum,String wxOrderNum,BigDecimal money ){
		Object[] params = new Object[]{wxOrderNum,money,orderNum};
		if(jdbcTemplate.update(Sql2zcConfig.editNorUserBillStatus, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 更新入账表状态
	 * @param orderNum
	 * @return
	 */
	public boolean updateLogIncomeStatus(String orderNum,String wxOrderNum,BigDecimal money ){
		Object[] params = new Object[]{wxOrderNum,money,orderNum};
		if(jdbcTemplate.update(Sql2zcConfig.updateLogIncomeStatus, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 更新账户余额
	 * @param orderNum
	 * @return
	 */
	public boolean updateUserBalance(BigDecimal money,String orderNum){
		Object[] params = new Object[]{money,orderNum};
		if(jdbcTemplate.update(Sql2zcConfig.updateUserBalance, params) != 0)
			return true;
		return false;
	}
	
	/**
	 * 查询用户扣费列表---后面动态分页加载
	 * @param userId
	 * @return
	 */
	public List<LogNoruserbill> queryUserBillList(Integer userId){
		final List<LogNoruserbill> userBillList = new ArrayList<>();
		Object[] params = new Object[]{userId};
		jdbcTemplate.query(Sql2zcConfig.queryLogNorUserBill, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				LogNoruserbill userbill = new LogNoruserbill();
				userbill.setLogNorUserBillBalance(rs.getBigDecimal("log_norUserBill_balance"));
				userbill.setLogNorUserBillMoney(rs.getBigDecimal("log_norUserBill_money"));
				userbill.setLogNorUserBillInSeq(rs.getString("log_norUserBill_inSeq"));
				userbill.setLogNorUserBillUpdateTime(rs.getTimestamp("log_norUserBill_updateTime"));
				userBillList.add(userbill);
			}
		});
		return userBillList;
	}
	
	/**
	 * 查询用户充值列表---后面动态分页加载
	 * @param userId
	 * @return
	 */
	public List<LogIncome> queryIncomeList(Integer userId){
		final List<LogIncome> incomeList = new ArrayList<>();
		Object[] params = new Object[]{userId};
		jdbcTemplate.query(Sql2zcConfig.queryLogincome, params,new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				LogIncome income = new LogIncome();
				income.setLogIncomeBalance(rs.getBigDecimal("log_income_balance"));
				income.setLogIncomeMoney(rs.getBigDecimal("log_income_money"));
				income.setLogIncomeChannel(rs.getInt("log_income_channel"));
				income.setLogIncomeCreateTime(rs.getTimestamp("log_income_createTime"));
				incomeList.add(income);
			}
		});
		return incomeList;
	}
}